package com.example.springboot09.shejimoshi;

/**
 * @ClassName 工厂方法模式
 * @Description TODO
 * @Author tianqinglong
 * @Date 14:35 2022/7/25
 * @Version 1.0
 **/
public class 工厂方法模式 {
    public static void main(String[] args) {
        Car car1 = new WuLingFactory().getCar();
        Car car2 = new TeslaFactory().getCar();
        System.out.println(CarFactory1.getwuling()+"--");
        System.out.println(CarFactory1.getTesla()+"--");
        car1.name();
        car2.name();
    }
}
class CarFactory1 {
    //方法一
    public static Car getCar(String car) {
        //如果添加其他类，需要修改下列代码，修改逻辑
        if (car.equals("五菱")) {
            return getwuling();
        }else if (car.equals("特斯拉")) {
            return getTesla();
        }else {
            return null;
        }
    }

    //出现问题 增加一个车呢？
    //方法二   不修改原逻辑的情况下
    public static Car getwuling() {
        return new WuLing();
    }
    public static Car getTesla() {
        return new Tesla();
    }
}
interface Car {
    void name();
}
interface CarFactory {
    Car getCar();
}
class WuLing implements Car{
    @Override
    public void name() {
        System.out.println("五菱");
    }
}
class WuLingFactory implements CarFactory {
    @Override
    public Car getCar() {
        return new WuLing();
    }
}
class Tesla implements Car{
    @Override
    public void name() {
        System.out.println("特斯拉");
    }
}
class TeslaFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new Tesla();
    }
}
